/*******************************************************************************
 * Filename: free_agent.js 
 *
 * This file contains common JavaScript funciton used throughout
 * the Fantasy Broomball website.  Many of the common functions 
 * are used to support frequent AJAX requests.
 *
 * Author: Phil Peterson
 * Date: 11/30/2009
 *
 ******************************************************************************/

var g_free_table;
var g_free_players = {};

function freeAgentLoad(){
    getFreeAgents();
    //Get a object filled with all of the free agents and stats
    loadFreeObj();
}


/**
 *
 *
 *
 */
function swapFreeButtonDown(elem){
    elem.style.cursor = "pointer";
    elem.style.backgroundImage = "url(images/trade/swapplayers_2.gif)";
}

/**
 *
 *
 *
 */
function swapFreeButtonUp(elem){
    elem.style.backgroundImage = "url(images/trade/swapplayers_1.gif)";
}

/**
 *
 *
 *
 *
 */
function loadFreeObj(){

    var rows = document.getElementById("free_agents_table").rows;
    var id;
    var temp;
    
    for (var i = 1; i < rows.length; i++){
        temp = rows[i].childNodes[1].childNodes[0].value.split("_");
        id = temp[1];
        g_free_players[id] = {
            p_name  : rows[i].childNodes[2].childNodes[0].innerHTML,
            saves   : rows[i].childNodes[4],
            assists : rows[i].childNodes[6],
            goals   : rows[i].childNodes[8],
            fan_pts : rows[i].childNodes[10]
	    };
	}
}

function getFreeAgents(){
    g_free_table = document.getElementById("free_agents_table").innerHTML;
}

function sortTable(elem){

    switch(elem.innerHTML) {
        case 'Player':
            alert("Player");
            break;
        case 'Saves':
            alert("Saves");
            break;
        case 'Assists':
            alert("assists");
            break;
        case 'Goals':        
            break;
        case 'Fan Pts':
            break;
        default:
            break;
    }   

}

function filterPlayers(elem) {

    var rows = document.getElementById("free_agents_table").rows;
    var player_name;
    
    //If the string is empty display all of the rows 
    if (elem.value == ""){
        //document.getElementById("free_agents_table").innerHTML = g_free_table;
        for (var i = 1; i < rows.length; i++)
            rows[i].style.display = "table-row"; 
    }
    
    else {
        for (var i = 1; i < rows.length; i++){
            player_name = rows[i].childNodes[2].childNodes[0].innerHTML;
            //Compare 
            if (elem.value.toLowerCase() != player_name.substring(0,elem.value.length).toLowerCase())
                rows[i].style.display = "none";
            else
                rows[i].style.display = "table-row";    
        }
    }
}

/**
 *
 *
 *
 *
 */
function swapPlayers(){

	//Get the name and id
	var check_list = document.getElementsByName("trade_for");
	var player_id = -1;
    var opp_name;
    var tempArr;
   
   	//Loop through the radio buttons until the one selected is found.  Save
	//the free agent player id and name that was selected
	for (var i = 0; i < check_list.length; i++){
		if (check_list[i].checked){
			tempArr = check_list[i].value.split("_");
			player_id = tempArr[1];
            opp_name = check_list[i].parentNode.parentNode.childNodes[2].childNodes[0].innerHTML;
            var index = i;
			break;
		}
	}

	//Check if user has selected a player from their team to swap for
	if (g_myteam_id == -1)
		alert("Please select a player from your team to drop");
	else if (player_id == -1)
		alert("Please select a player from the free agent pool to sign");
		
	//Otherwise the trade can be completed
	else {   
    	//Prompt owner to confirm desire for trade
    	var response = confirm("Are you sure you want to sign " + g_myteam_name + " and drop " + opp_name);
    	
    	//Check if cancel was pressed
    	if (!response)
    	    return;

	    //Need to add the test request into the trade table in the database. Use an HTTP request to send the
	    //request to the server
	    var type        = "freeAgent";
	    var param_names = "type,team1,player1,free_agent";
	    var param_vals  = type + "," + g_team_id + "," + g_myteam_id + "," + player_id;
	    var url         = "request_data.php";

	    response = postRequest(param_names, param_vals, url);
	
	    //Reset the rink players and the radio button
	    document.getElementById(g_player_doc_id).src = "images/rink/red_player_down.png";
        //Set the name and player on the player image
        document.getElementById("trade_team_name_home").innerHTML = "";
        document.getElementById("trade_player_name_home").innerHTML = "";
        document.getElementById("trade_team_name_opp").innerHTML = "";
        document.getElementById("trade_player_name_opp").innerHTML = "";        

	    var label = document.getElementById(g_player_doc_id + "_label");
	    label.style.backgroundColor = "#E2AFAF";
	    label.style.color = "#000000";  
	    
	    check_list[index].checked = "";

		alert(response);

		//Refresh page to pull down new roster
		window.location.reload();
    }
}
